<?php

namespace App\Http\Controllers\Admin;

use App\Http\Model\Permission;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;

class PermissionController extends Controller
{

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {


        $data =  Permission::get();
        return view('admin.permission.index',compact('data'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        return view('admin.permission.add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
        $input =  Input::except('_token');
        $permission = Permission::create($input);
        if($permission){
            return redirect('admin/permission');
        }else{
            return back()->with('errors','添加失败');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //1 获取到当前角色
        $permission =   permission::find($id);
//        2 获取到所有的权限
        $pers = Permission::get();
//        获取当前角色已经被授予的权限
        $own_pers=  \DB::table('permission_permission')->where('permission_id',$id)->lists('permission_id');
        //dd($own_pers);
//        3 将相关的数据（角色、权限）返回给授权页面
        return view('admin.permission.auth',compact('permission','pers','own_pers'));

    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        $permission = Permission::find($id);
        return view('admin.permission.edit',compact('permission'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
        $input = Input::except('_method','_token');
        $permission = Permission::find($id);
        $re = $permission->update($input);
        if($re){
            return redirect('admin/permission');
        }else{
            return back()->with('errors','修改失败');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        $re =  Permission::find($id)->delete();
        if($re){
            $data =[
                'status'=>0,
                'msg'=>'删除成功'
            ];
        }else{
            $data =[
                'status'=>1,
                'msg'=>'删除失败'
            ];
        }
        return $data;
    }
}
